package com.tulingxueyuan.mall.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tulingxueyuan.mall.common.api.CommonResult;
import com.tulingxueyuan.mall.modules.pms.model.PmsProduct;
import com.tulingxueyuan.mall.modules.pms.model.PmsProductAttribute;
import com.tulingxueyuan.mall.modules.pms.model.PmsProductAttributeValue;
import com.tulingxueyuan.mall.modules.pms.model.PmsSkuStock;
import com.tulingxueyuan.mall.dto.PmsProductAttributeValueDto;
import com.tulingxueyuan.mall.dto.ProductSaveDto;
import com.tulingxueyuan.mall.modules.pms.service.PmsProductAttributeService;
import com.tulingxueyuan.mall.modules.pms.service.PmsProductAttributeValueService;
import com.tulingxueyuan.mall.modules.pms.service.PmsProductService;
import com.tulingxueyuan.mall.modules.pms.service.PmsSkuStockService;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@Api(tags = "ProductController",description = "商品详情服务接口")
@RequestMapping("/product")
public class ProductController {
    @Autowired
    PmsProductService productService;
    @Autowired
    PmsSkuStockService skuStockService;
    @Autowired
    PmsProductAttributeValueService attributeValueService;
    @Autowired
    PmsProductAttributeService productAttributeService;
    @GetMapping("/detail/{id}")
    public CommonResult getProductDetail(@PathVariable Long id){
        PmsProduct product = productService.getById(id);
        ProductSaveDto productSaveDto = new ProductSaveDto();
        BeanUtils.copyProperties(product,productSaveDto);

        LambdaQueryWrapper<PmsSkuStock> lqw = new LambdaQueryWrapper<>();
        lqw.eq(PmsSkuStock::getProductId,id);
        List<PmsSkuStock> skuStockList = skuStockService.list(lqw);
        productSaveDto.setSkuStockList(skuStockList);

        LambdaQueryWrapper<PmsProductAttributeValue> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(PmsProductAttributeValue::getProductId,id);
        List<PmsProductAttributeValue> attributeValueList = attributeValueService.list(lqw1);
//        属性和参数名称对应   创造dto类  PmsProductAttributeValueDto
       List<PmsProductAttributeValueDto> ProductAttributeValueDtoList =attributeValueList.stream().map(e->{
            PmsProductAttributeValueDto pmsProductAttributeValueDto = new PmsProductAttributeValueDto();
            BeanUtils.copyProperties(e,pmsProductAttributeValueDto);
            LambdaQueryWrapper<PmsProductAttribute> lqw2 = new LambdaQueryWrapper<>();
            lqw2.eq(PmsProductAttribute::getId,e.getProductAttributeId());
            PmsProductAttribute productAttribute = productAttributeService.getOne(lqw2);
            pmsProductAttributeValueDto.setCateName(productAttribute.getName());
            return pmsProductAttributeValueDto;
        }).collect(Collectors.toList());
        productSaveDto.setProductAttributeValueDtoList(ProductAttributeValueDtoList);
        return CommonResult.success(productSaveDto);
    }
}
